package com.bestcem.xm.survey.controller;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.survey.controller.param.QuotaProgressParam;
import com.bestcem.xm.survey.service.SurveyQuotaProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "配额进度")
@RestController
@RequiredArgsConstructor
@RequestMapping("/survey/")
public class SurveyQuotaProgressController {
    private @NonNull SurveyQuotaProgressService surveyQuotaProgressService;

    @ApiOperation(value = "配额分享")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress/share"})
    public XmResultJson<JSONObject> getQuotaProgressShare(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaProgressService.getQuotaProgressShare(projectId));
    }

    @ApiOperation(value = "配额进度页面")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress"})
    public XmResultJson<JSONObject> getQuotaProgress(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaProgressService.getQuotaProgress(projectId));
    }

    @ApiOperation(value = "配额进度分享页面")
    @AuthIgnore
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress/public"})
    public XmResultJson<JSONObject> getQuotaProgressPublic(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaProgressService.getQuotaProgressPublic(projectId));
    }

    @ApiOperation(value = "分享链接验证")
    @AuthIgnore
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress/share/valid/public"})
    public XmResultJson<JSONObject> getQuotaProgressValid(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaProgressService.getQuotaProgressValid(projectId));
    }

    @ApiOperation(value = "校验密码")
    @AuthIgnore
    @PostMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress/share/valid/public"})
    public XmResultJson<JSONObject> checkQuotaProgressValid(@PathVariable(value = "projectId") String projectId, @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaProgressService.checkQuotaProgressValid(projectId, param));
    }

    @ApiOperation(value = "配额进度分享-保存配置")
    @PostMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/progress/share"})
    public XmResultJson<JSONObject> saveQuotaProgress(@PathVariable(value = "projectId") String projectId,
                                                @RequestBody QuotaProgressParam param) {
        String orgId = SecurityContextHolder.getOrgId();
        String userId = SecurityContextHolder.getUserId();
        param.setUserId(userId);
        param.setOrgId(orgId);
        return XmResultJson.success(surveyQuotaProgressService.saveQuotaProgress(projectId, param));
    }
}